// pages/register/index.js
import config from '../../etc/config.js'
const {
  $Toast
} = require('../../components/dist/base/index');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopId: '',
    servicesValue: '',
    disabled: false,
    hiddennoPay: true,
    checkItems: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.hideShareMenu()
    if (options.value) {
      let value = JSON.parse(options.value)
      this.setData({
        info: value
      })
    } else {
      this.setData({
        info: {}
      })
    }
    this.getAllShops()
    // this.getAllServesTypes()
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },
  phone: function(phone) {
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (phone.length == 0) {
      $Toast({
        content: '输入的手机号为空'
      })
      return false;
    } else if (phone.length < 11) {
      $Toast({
        content: '手机号长度有误！'
      })
      return false;
    } else if (!myreg.test(phone)) {
      $Toast({
        content: '请输入正确的手机号！'
      })
      return false;
    }
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  submitRegister(e) {
    var that = this
    this.setData({
      disabled: true
    })
    var str = '',
      cate = '',
      value = ''
    for (let i = 0; i < that.data.checkItems.length; i++) {
      cate = that.data.checkItems[i].categoryId
      value = ''
      for (let j = 0; j < that.data.checkItems[i].brand.length; j++) {
        if (j == that.data.checkItems[i].brand.length - 1) {
          value += '"' + that.data.checkItems[i].brand[j].brandId + '"'
        } else {
          value += '"' + that.data.checkItems[i].brand[j].brandId + '",'
        }
      }
      if (i == that.data.checkItems.length - 1) {
        str += '{"cate":"' + cate + '",' + '"value":[' + value + ']}'
      } else {
        str += '{"cate":"' + cate + '",' + '"value":[' + value + ']},'
      }
    }
    let token = wx.getStorageSync('token')
    let params = {
      userId: wx.getStorageSync('userId'),
      deName: e.detail.value.name ? e.detail.value.name : that.data.info.deName,
      dePhone: e.detail.value.phone ? e.detail.value.phone : that.data.info.dePhone,
      shopId: that.data.shopId,
      deServicesValue: that.data.servicesValue,
      deBankName: e.detail.value.bankName ? e.detail.value.bankName : that.data.info.deBankName,
      deBank: e.detail.value.bank ? e.detail.value.bank : that.data.info.deBank,
      deBankCard: e.detail.value.bankNumber ? e.detail.value.bankNumber : that.data.deBankCard,
      jsonStr: '[' + str + ']',
    }
    if (that.data.info) {
      params.id = that.data.info.id
      params.checkStatus = that.data.info.checkStatus
    }
    if (!params.deName) {
      $Toast({
        content: '请填写姓名'
      }, 2000)
      that.setData({
        disabled: false
      })
      return false
    }
    if (!params.dePhone) {
      $Toast({
        content: '请填写正确联系方式'
      })
      that.setData({
        disabled: false
      }, 2000)
      return false
    }
    if (!params.shopId) {
      $Toast({
        content: '请选择所属门店'
      }, 2000)
      that.setData({
        disabled: false
      })
      return false
    }
    if (!params.deServicesValue) {
      $Toast({
        content: '请选择服务类目'
      }, 2000)
      that.setData({
        disabled: false
      })
      return false
    }
    if (!params.deBankName || !params.deBank || !params.deBankCard) {
      $Toast({
        content: '银行卡信息有缺失'
      }, 2000)
      that.setData({
        disabled: false
      })
      return false
    }

    wx.showLoading({
      title: '加载中...',
      success: function() {
        config._post('/deliveryman/register', token, params).then(res => {
          console.log(token)
          if (res.code == 0) {
            wx.showModal({
              title: '提示',
              content: '申请已提交，请等待审核',
              showCancel: false,
              success(res) {
                if (res.confirm) {
                  wx.navigateBack()
                }
              }
            })
          } else {
            wx.hideLoading();
            $Toast({
              content: res.msg
            })
          }
          that.setData({
            disabled: false
          })
        })
      }
    })
  },
  getAllShops() {
    config._post('/shop/getAllShops').then(res => {
      this.setData({
        shopslist: res.data
      })
    })
  },
  getAllServesTypes() {
    config._get('/merServicesType/allMerServiceTypes').then(res => {
      this.setData({
        serveslist: res.data
      })
    })
  },
  bindShopChange(e) {
    this.setData({
      shopIndex: e.detail.value
    })
    this.data.shopId = this.data.shopslist[e.detail.value].id

    let token = wx.getStorageSync('token')
    config._post('/merServicesType/getMerTypeByShopId', token, {
        shopId: this.data.shopId
      })
      .then(res => {
        this.setData({
          serveslist: res.data
        })
      })
  },
  bindServesChange(e) {
    let val = this.data.serveslist[e.detail.value]
    this.setData({
      servesIndex: e.detail.value,
      serviceType: val.type
    })
    this.data.servicesValue = val.id
  },
  selectCateAndBrand() {
    if (this.data.shopId) {
      wx.navigateTo({
        url: '/pages/cate-brand/index?shopId=' + this.data.shopId + '&typeId=' + this.data.servicesValue + '&type=' + this.data.serviceType + '&checkItems=' + JSON.stringify(this.data.checkItems),
      })
    } else {
      $Toast({
        content: '请选择所属门店'
      })
    }
  }
})